package cn.kduck.code.entity.service;

import cn.kduck.core.service.ValueMap;

import java.util.Map;

import static cn.kduck.code.entity.service.EntityField.EntityFieldFieldEnum.*;

public class EntityField extends ValueMap {

    public static final int FORM_ITEM_TYPE_INPUT = 1; //文本框
    public static final int FORM_ITEM_TYPE_TEXTAREA = 2; //多行文本框
    public static final int FORM_ITEM_TYPE_SELECT =3; //下拉单框
    public static final int FORM_ITEM_TYPE_RADIO_GROUP =4; //单选框
    public static final int FORM_ITEM_TYPE_CHECK_BOX =5; //复选框
    public static final int FORM_ITEM_TYPE_MULTIPLE_SELECT =6; //下拉多选框
    public static final int FORM_ITEM_TYPE_DATE =7; //日期选择框
    public static final int FORM_ITEM_TYPE_COLOR =9; //颜色选择框
    public static final int FORM_ITEM_TYPE_HIDDEN =10; //隐藏域
    public static final int FORM_ITEM_TYPE_DIALOG =11; //选择框

    public EntityField() {
    }

    public EntityField(Map map) {
        if (map != null) {
            super.putAll(map);
        }
    }

    public void setEntityId(String entityId) {
        super.put(ENTITY_ID.getAttrName(), entityId);
    }

    public String getEntityId() {
        return super.getValueAsString(ENTITY_ID.getAttrName());
    }

    public void setFieldId(String fieldId) {
        super.put(FIELD_ID.getAttrName(), fieldId);
    }

    public String getFieldId() {
        return super.getValueAsString(FIELD_ID.getAttrName());
    }

    public void setFieldName(String fieldName) {
        super.put(FIELD_NAME.getAttrName(), fieldName);
    }

    public String getFieldName() {
        return super.getValueAsString(FIELD_NAME.getAttrName());
    }

    public void setFieldCode(String fieldCode) {
        super.put(FIELD_CODE.getAttrName(), fieldCode);
    }

    public String getFieldCode() {
        return super.getValueAsString(FIELD_CODE.getAttrName());
    }

    public void setFieldType(Integer fieldType) {
        super.put(FIELD_TYPE.getAttrName(), fieldType);
    }

    public Integer getFieldType() {
        return super.getValueAsInteger(FIELD_TYPE.getAttrName());
    }

    public void setFieldLength(Integer fieldLength) {
        super.put(FIELD_LENGTH.getAttrName(), fieldLength);
    }

    public Integer getFieldLength() {
        return super.getValueAsInteger(FIELD_LENGTH.getAttrName());
    }

    public void setFieldScale(Integer fieldScale) {
        super.put(FIELD_SCALE.getAttrName(), fieldScale);
    }

    public Integer getFieldScale() {
        return super.getValueAsInteger(FIELD_SCALE.getAttrName());
    }

    public void setDictCategoryCode(String dictCategoryCode) {
        super.put(DICT_CATEGORY_CODE.getAttrName(), dictCategoryCode);
    }

    public String getDictCategoryCode() {
        return super.getValueAsString(DICT_CATEGORY_CODE.getAttrName());
    }

    public void setIsKey(Boolean isKey) {
        super.put(IS_KEY.getAttrName(), isKey);
    }

    public Boolean getIsKey() {
        return super.getValueAsBoolean(IS_KEY.getAttrName());
    }

    public void setIsNullable(Boolean isNullable) {
        super.put(IS_NULLABLE.getAttrName(), isNullable);
    }

    public Boolean getIsNullable() {
        return super.getValueAsBoolean(IS_NULLABLE.getAttrName());
    }

    public void setIsUnique(Boolean isUnique) {
        super.put(IS_UNIQUE.getAttrName(), isUnique);
    }

    public Boolean getIsUnique() {
        return super.getValueAsBoolean(IS_UNIQUE.getAttrName());
    }

    public void setCanEdit(Boolean canEdit) {
        super.put(CAN_EDIT.getAttrName(), canEdit);
    }

    public Boolean getCanEdit() {
        return super.getValueAsBoolean(CAN_EDIT.getAttrName());
    }

    public void setCanVisible(Boolean canVisible) {
        super.put(CAN_VISIBLE.getAttrName(), canVisible);
    }

    public Boolean getCanVisible() {
        return super.getValueAsBoolean(CAN_VISIBLE.getAttrName());
    }

    public void setCanDelete(Boolean canDelete) {
        super.put(CAN_DELETE.getAttrName(), canDelete);
    }

    public Boolean getCanDelete() {
        return super.getValueAsBoolean(CAN_DELETE.getAttrName());
    }

    public void setFormItemType(Integer formItemType) {
        super.put(FORM_ITEM_TYPE.getAttrName(), formItemType);
    }

    public Integer getFormItemType() {
        return super.getValueAsInteger(FORM_ITEM_TYPE.getAttrName());
    }

    public void setIsConditionField(Boolean isConditionField) {
        super.put(IS_CONDITION_FIELD.getAttrName(), isConditionField);
    }

    public Boolean getIsConditionField() {
        return super.getValueAsBoolean(IS_CONDITION_FIELD.getAttrName());
    }

    public void setOrderNum(Integer orderNum) {
        super.put(ORDER_NUM.getAttrName(), orderNum);
    }

    public Integer getOrderNum() {
        return super.getValueAsInteger(ORDER_NUM.getAttrName());
    }

    public void setIsIndexField(Boolean isIndexField) {
        super.put(IS_INDEX_FIELD.getAttrName(), isIndexField);
    }

    public Boolean getIsIndexField() {
        return super.getValueAsBoolean(IS_INDEX_FIELD.getAttrName());
    }

    public enum EntityFieldFieldEnum {
        ENTITY_ID("ENTITY_ID", "entityId"),
        FIELD_ID("FIELD_ID", "fieldId"),
        FIELD_NAME("FIELD_NAME", "fieldName"),
        FIELD_CODE("FIELD_CODE", "fieldCode"),
        FIELD_TYPE("FIELD_TYPE", "fieldType"),
        FIELD_LENGTH("FIELD_LENGTH", "fieldLength"),
        FIELD_SCALE("FIELD_SCALE", "fieldScale"),
        DICT_CATEGORY_CODE("DICT_CATEGORY_CODE", "dictCategoryCode"),
        IS_KEY("IS_KEY", "isKey"),
        IS_NULLABLE("IS_NULLABLE", "isNullable"),
        IS_UNIQUE("IS_UNIQUE", "isUnique"),
        CAN_EDIT("CAN_EDIT", "canEdit"),
        CAN_VISIBLE("CAN_VISIBLE", "canVisible"),
        CAN_DELETE("CAN_DELETE", "canDelete"),
        FORM_ITEM_TYPE("FORM_ITEM_TYPE", "formItemType"),
        IS_CONDITION_FIELD("IS_CONDITION_FIELD", "isConditionField"),
        ORDER_NUM("ORDER_NUM", "orderNum"),
        IS_INDEX_FIELD("IS_INDEX_FIELD", "isIndexField");

        private final String fieldName;
        private final String attrName;

        EntityFieldFieldEnum(String fieldName, String attrName) {
            this.fieldName = fieldName;
            this.attrName = attrName;
        }

        public String getFieldName() {
            return fieldName;
        }

        public String getAttrName() {
            return attrName;
        }
    }

    @Override
    public String toString() {
        return "字段说明：" + getFieldName() +"，表字段名：" + getFieldCode() + "，类型：" + getFieldTypeName() + "长度：" + getFieldLength() + "，精度：" + getFieldScale() +"\r\n";
    }

    private String getFieldTypeName() {
        switch (getFieldType()) {
            case 1:
                return "字符串";
            case 2:
                return "整型";
            case 3:
                return "浮点型";
            case 4:
                return "日期";
            case 5:
                return "时间";
            case 6:
                return "日期时间";
            case 7:
                return "布尔型";
            case 8:
                return "二进制";
        }
        return "未知";
    }
}
